#!/usr/bin/env python2.5
# -*- coding: UTF-8 -*-  
 

__author__ = "Nick Milon"

 
import models
db =models.db
users = models.ApplUser
ApplService = models.ApplService
ApplUser=models.ApplUser
from google.appengine.ext import webapp

from google.appengine.ext.webapp.util import run_wsgi_app
 
#from google.appengine.api.labs import taskqueue 
#from google.appengine.api import urlfetch 
#import logging
#import hashlib
#import time   
    

#def tropoFetch(token, smsUID):
#    _baseUri="http://api.tropo.com/1.0/sessions" 
#    uriTropo="%s?token=%s&action=create&smsUID=%s" %(_baseUri, token, smsUID)
#    rpc = urlfetch.create_rpc(deadline=10)
#    logging.info("Fetching url:"+ uriTropo)
#    urlfetch.make_fetch_call(rpc, uriTropo, payload=None, method="GET", headers={}, allow_truncated=False, follow_redirects=False) 
#    try:
#        result = rpc.get_result() 
#        if result.status_code == 200:
#            logging.info ("tropoFetch succes"+str(result.status_code))
#            logging.info ('tropoFetch content'+str(result.content) )
#    except:# urlfetch.DownloadError:
#        logging.error ("tropoFetch failed:"+str(result.status_code))     
resp="""
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >

  <form>
    <block>
    <prompt>
      Hi! Lilya this is your bank speaking.
      We want to inform you that your outstanding balance is $40000.
      Your acoount expires in 30 days.
      Thank you.
      Bye.
    </prompt>
    </block>
  </form>
</vxml> 
""" 

class RQVoxeo(webapp.RequestHandler): 
    def prepareResponce(self):
        self.response.headers['mimetype'] ="application/json"
        
    def get(self, arg1, arg2):  
        self.response.out.write(resp)
                  
        
#class RQHsmsTQ(webapp.RequestHandler): 
#    def post(self):
#        #logging.info("request:"+str(self.request))
#        msgToNum=self.request.get('msgToNum', None)
#        msgTxt=self.request.get('msgTxt', None)
#        smsUID=self.request.get('smsUID', None) 
#        logStr="smsUID:%s,msgToNum:%s,msgTxt:%s" %(smsUID, msgToNum, msgTxt)
#        logging.info(logStr) 
#        smsEntity= models.msgSMS(key_name=smsUID, msgToNum=msgToNum, msgTxt=msgTxt, status=1) 
#        smsEntity.put() 
#        tropoFetch(TropoApplTokenMessage, smsUID) 
        

application = webapp.WSGIApplication(
                                     [ 
                                    (r"/voxeo/(.*?)/(.*?)", RQVoxeo), 
                                    ('/', RQVoxeo)],
                                     debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == "__main__":
    main()
